#include "gofgame/dungeonkeeper/game/unit/DarkMage.h"
#include "AnimatePacker/AnimatePacker.h"
#include "gofgame/dungeonkeeper/game/GameManager.h"
#include "gofgame/dungeonkeeper/game/other/FlyItem.h"

using namespace gofgame::dungeonkeeper::game::unit;
using namespace gofgame::dungeonkeeper::game;
using namespace gofgame::dungeonkeeper::game::other;

DarkMage::DarkMage(){
	id = 106;
}

DarkMage::~DarkMage(){
}

bool DarkMage::init(){
	bool pRet = false;
	do 
	{
		unitType = UnitType::createById(this->id);
		texture = CCSprite::create(unitType.imageName);
		this->addChild(texture);
		updateState();
		Unit::init();
		initMyAnimate();
		pRet = true;
	} while (0);
	return pRet;
}

void DarkMage::initMyAnimate(){
	AnimatePacker *ap = AnimatePacker::getInstance();
	ap->loadAnimations("effect/fireball.xml");
	flayItemAnimate = ap->getAnimate("play");
	flayItemAnimate->retain();
}

DarkMage * DarkMage::createByLevel(int lv){
	DarkMage *darkMage = new DarkMage();
	if(darkMage){
		darkMage->lv = lv;
		if( darkMage->init()){
			return darkMage;
		} else {
			return nullptr;
		}
	} else{
		return nullptr;
	}
}

void DarkMage::initData(){
	this->origHp = lv * 20 + unitType.getBaseHP();
	this->currentHp = origHp;
	this->atk = lv * 5 + unitType.getBaseAtk();
	this->def = lv * 5 + unitType.getBaseDef();
	this->speed = unitType.getBaseSpeed();
	this->attkSpeed = unitType.getBaseAttackSpeed();
	this->range = unitType.getBaseRange();
}

void DarkMage::attack(Target *target){
	if(this->attackTarget != nullptr) {
		this->playAttatckAni();
		FlyItem *fi = FlyItem::create(this,target);
		fi->go();
		target->hurted(this->getAtk());
	}
}


void DarkMage::playFlyItem(CCPoint point){
	CCSprite *flyItem = CCSprite::create("S_death.png");
	flyItem->setPosition(this->getPosition());
	GameManager::sharedGameManager()->getEffectLayer()->addChild(flyItem);
//	flyItem->runAction(this->flayItemAnimate);
//	flyItem->runAction(CCSequence::create(CCMoveTo::create(1.5f,point),callfuncND_selector(DarkMage::onFlyItemFinish),(void*)flyItem,NULL));
	flyItem->runAction(CCMoveTo::create(1.0f,point));

}

void DarkMage::onFlyItemFinish(CCNode *psender,void* data) {
	CCSprite *flyItem = (CCSprite*)data;
	GameManager::sharedGameManager()->getEffectLayer()->removeChild(flyItem,true);
}


void DarkMage::doAI(){

}
